package utils;

import entity.News;
import entity.NewsInfo;
import org.apache.log4j.Logger;

import java.io.FileInputStream;
import java.io.IOException;
import java.sql.*;
import java.util.*;

/**
 * Created by 梅晨 on 2017/7/31.
 */
public class ClfyObj {
    private static Logger logger = Logger.getLogger(ClfyObj.class);
    HashMap<String,Integer> newsPath;
    String emotionmapPath;
    String categorymapPath;
    String emotionAccuracyFilePath;
    String categoryAccuracyFilePath;

    public ClfyObj(HashMap<String,Integer> newsPath){
        this.newsPath = newsPath;
        init();
    }

    private void init(){
        Properties properties = new Properties();
        try{
            properties.load(new FileInputStream("src\\main\\resources\\PathConfig.properties"));
            emotionAccuracyFilePath = properties.getProperty("emorionAccuracyFilePath");
            emotionmapPath = properties.getProperty("emotionMapPath");
            categoryAccuracyFilePath = properties.getProperty("categoryAccuracyFilePath");
            categorymapPath = properties.getProperty("categorymapPath");

        }catch (IOException e){
            e.printStackTrace();
        }
    }

    public void newObj()throws IOException{
        logger.info("create newsinfo object");
        List<NewsInfo> newsList = new ArrayList<NewsInfo>();
        for(String path : newsPath.keySet()){
            String[] ss = path.split("\\\\");
            String title = ss[ss.length - 1];
            String content = IOUtils.getFileContent(path);
            NewsInfo newsInfo = new NewsInfo(title,content);
            newsList.add(newsInfo);
        }

        logger.info("load classify result and it's map");
        HashMap<Integer,String> emotionMap = IOUtils.readMap(emotionmapPath);
        HashMap<Integer,String> categoryMap = IOUtils.readMap(categorymapPath);
        List<Integer> emotionresult = IOUtils.readClassId(emotionAccuracyFilePath);
        List<Integer> categoryresult = IOUtils.readClassId(categoryAccuracyFilePath);


        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new java.util.Date());
        calendar.add(Calendar.DATE,0);
        java.sql.Date date = new java.sql.Date(calendar.getTime().getTime());
        if(newsList.size() == emotionresult.size() && newsList.size() ==  categoryresult.size()){
            for(int i = 0; i < newsList.size(); i++){
                NewsInfo newsInfo = newsList.get(i);
                newsInfo.setEmotion(emotionMap.get(emotionresult.get(i)));
                newsInfo.setCategory(categoryMap.get(categoryresult.get(i)));
                newsInfo.setDate(date);
                System.out.println(newsInfo.getTitle() + " : " + newsInfo.getCategory() + " : " + newsInfo.getEmotion());
            }
        }else{
            logger.error("the length of input is not equal to output");
        }
        HashMap<String,Set<NewsInfo>> newsCtgyMap = new HashMap<String, Set<NewsInfo>>();
        for(NewsInfo newsInfo : newsList){
            String category = newsInfo.getCategory();
            if(newsCtgyMap.containsKey(category)){
                newsCtgyMap.get(category).add(newsInfo);
            }else {
                Set<NewsInfo> newsSet = new HashSet<NewsInfo>();
                newsSet.add(newsInfo);
                newsCtgyMap.put(category,newsSet);
            }
        }
        Share2SQL.run();
        HashMap<String,Integer> score = Share2SQL.score;
        SqlUtil sqlUtil = new SqlUtil();
        sqlUtil.insertNews(newsList);
        sqlUtil.insertEmotion(newsCtgyMap,score);
    }
}
